package com.szgr.baoyin.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.utils.Constant;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 申请会员
 */
public class VipApplyActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener, TextWatcher {

    private Button btn_submit;
    private EditText et_money;
    private TextView tv_rule;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 333:
                    tv_cardLevel.setText("无");
                    break;
                case 334:
                    tv_cardLevel.setText((String)msg.obj);
                    break;
                case 335:
                    Intent intent = new Intent(VipApplyActivity.this,WebActivity.class);
                    intent.putExtra("html",(String)msg.obj);
                    startActivity(intent);
                    finish();
                    break;
            }
        }
    };
    private TextView tv_cardLevel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_vip_apply);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("申请会员");
    }

    @Override
    public void initView() {
        EditText et_applyUser = (EditText) findViewById(R.id.vip_apply_user);
        EditText et_cardNo = (EditText) findViewById(R.id.vip_apply_card_no);
        EditText et_phone = (EditText) findViewById(R.id.vip_apply_phone);
        EditText et_address = (EditText) findViewById(R.id.vip_apply_address);
        EditText et_cardAddress = (EditText) findViewById(R.id.vip_apply_card_address);
        EditText et_box = (EditText) findViewById(R.id.vip_apply_box);
        tv_rule = (TextView) findViewById(R.id.vip_apply_rule);
        CheckBox cb_agree = (CheckBox) findViewById(R.id.vip_apply_check);
        cb_agree.setOnCheckedChangeListener(this);
        RadioButton rb_pay01 = (RadioButton) findViewById(R.id.vip_apply_pay_01);
        RadioButton rb_pay02 = (RadioButton) findViewById(R.id.vip_apply_pay_02);
        et_money = (EditText) findViewById(R.id.vip_apply_money);
        tv_cardLevel = (TextView) findViewById(R.id.vip_apply_card_level);
        btn_submit = (Button) findViewById(R.id.vip_apply_submit);
        Button btn_back = (Button) findViewById(R.id.vip_apply_back);

        tv_rule.setOnClickListener(this);
        et_money.addTextChangedListener(this);
        btn_submit.setOnClickListener(this);
        btn_back.setOnClickListener(this);
    }

    private void getLevel(final String pdr_amount) {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_LEVEL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        System.out.println("---level--->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONObject datas = jsonObject.getJSONObject("datas");
                            if(datas.has("error")){
                                mHandler.obtainMessage(333).sendToTarget();
                            }else if(datas.has("level")){
                                JSONObject level = datas.getJSONObject("level");
                                String level_name = level.getString("level_name");
                                mHandler.obtainMessage(334,level_name).sendToTarget();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("pdr_amount",pdr_amount);
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.vip_apply_back:
            case R.id.action_bar_left:
                finish();
                break;
            case R.id.vip_apply_submit:
                recharge();
                break;
            case R.id.vip_apply_rule:
                Intent ruleIntent = new Intent(this, VipRuleActivity.class);
                ruleIntent.putExtra("from","apply");
                startActivity(ruleIntent);
                break;
        }
    }

    private void recharge() {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_RECHARGE,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        System.out.println("----recharge--->" + s);
                        mHandler.obtainMessage(335,s).sendToTarget();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key",MyApplication.getmInstance().getKey());
                map.put("pdr_amount",et_money.getText().toString().trim());
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
            btn_submit.setEnabled(true);
        }else{
            btn_submit.setEnabled(false);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(s != null && s.length() > 0){
            getLevel(s.toString());
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
